<?php
namespace Home\Controller;
use Think\Controller;
class DatasettingController extends BackController {
	public function DataAllocation(){
			$akey = array(1=>'非常重点',2=>'重点',3=>'一般',4=>'未知');
			if (session('qe_user')['pwdid'] == 1){
			    //admin
                $uwhere['pwdid']  = array('in',array('7','3','4'));
            }else{
                $uwhere['pwdid']  = array('in',array('3','4','7'));
                $uwhere['xid'] = array('eq',session('qe_user')['xid'],'and');
            }

			$user = M('user')->where($uwhere)->select();
			$all_am = $this->all_am();
			$this->assign('all_am',$all_am);
			$this->assign('sc_user',$user);
			$this->assign('akey',$akey);
			if(IS_POST){
				$sc_student = M('sc_student');
				$where="";
				$scid = I('post.scid',0,'int');
				$am_id = I('post.am_id','','int');
				$akey_id = I('post.akey_id','','int');
				$pwdid = M('user')->where('id='.$scid)->find();
				//查询名单建立人,跟踪人
				$where['xsid'] = array('eq',$scid,'and');
				$this->assign('scid',$scid);
				//来源
				if(!empty($am_id)){
					$where['am_id'] = array('eq',$am_id,'and');
					$this->assign('am_id',$am_id);
				}
				//重点
				if(!empty($akey_id)){
					$where['akey_id'] = array('eq',$akey_id,'and');
					$this->assign('akey_id',$akey_id);
				}
				$phone = I('post.phone');
				//联系手机
				if(!empty($phone)){
					$where['phone'] = array('eq',$phone,'and');
					$this->assign('phone',$phone);
				}
				//就读学校
				$school = I('post.school','','addslashes');
				if(!empty($school)){
					$where['school'] = array('like','%'.$school.'%','and');
					$this->assign('school',$school);
				}
				//出生年月
				$birth_date = I('post.birth_date','','addslashes');
				if(!empty($birth_date)){
					$where['birth_date'] = array('eq',$birth_date,'and');
					$this->assign('birth_date',$birth_date);
				}
				$content = I('post.content','','addslashes');
				$this->assign('content',$content);
				//$where['dt_id'] = array('neq',1,'and');
				$sc_data = $sc_student->where($where)->order('id desc')->limit(50)->select();
				if(!empty($content)){ 
					foreach($sc_data as $k=>$v){
						$sc_data[$k]['record_data'] = $this->record_data($v['id']);
						$sc_data[$k]['sc_name'] = $this->username($v['scid']);
						$sc_data[$k]['am_name'] = $this->basname($v['am_id']);
						$sc_data[$k]['x_name'] = $this->basname($v['xid']);
					}
					foreach($sc_data as $key=>$val){
						if(!empty($val['record_data'])){
							$ss[$key] = $val;
						}
					}
					$sc_data = $ss;
				}else{
					foreach($sc_data as $k=>$v){
						$sc_data[$k]['record_data'] = $this->record_data($v['id']);
						$sc_data[$k]['sc_name'] = $this->username($v['scid']);
						$sc_data[$k]['am_name'] = $this->basname($v['am_id']);
						$sc_data[$k]['x_name'] = $this->basname($v['xid']);
					}
				}
				$this->assign('sc_data',$sc_data);
				//var_dump($sc_data);
			}
			$this->display();
		
	}
    public function studentadd(){
        $sc_student = M('sc_student');
        if(IS_POST){
            $data['xsid'] = I('post.xsid','','int');
            if (!$data['xsid']){
                $this->error('分配对象不能为空');
            }
            /*9-13*/
            $user = M('user')->where('id='.$data['xsid'])->find();
            $data['xid'] = $user['xid'];
            /*9-13*/
            $all = I('post.all','','addslashes');
            if (empty($all)){
                $this->error('请选择被分配对象!',U('Datasetting/DataAllocation'));
            }
            if(empty($data['xsid'])){
                $this->error('请选择分配对象!',U('Datasetting/DataAllocation'));
            }else{
                $where['xsid'] = array('eq',$data['xsid'],'and');
                $where['state_id']  = array('not in',array('9','13'));
                $all_count = $this->all_count(2);
                //查询是否为校长
                $is_xiaozhang = M('user')->where('id='.$data['xsid'])->find();
                foreach($all as $v){
                    $count = $sc_student->where($where)->count();
                    if($count<$all_count || $is_xiaozhang['pwdid'] == 7){
                        $sc_student->where('id='.$v)->save($data);
                        $ss = true;
                    }else{
                        $ss = false;
                    }
                }
                if($ss == true){
                    $this -> success('分配成功！',U('Datasetting/DataAllocation'));
                }else{
                    $this->error('该销售名单已满,请重新分配!',U('Datasetting/DataAllocation'));
                }
            }
        }
    }
	 //报名咨询人数分析统计图
    public function counselling()
    {

        $total_enroll_data = M('school_area')->select();

        foreach ($total_enroll_data as $k => $v) {
            $school_area[] = $v['abbreviation'];
            $total_enroll[] = M('sc_student')->where(['xid' => $v['id']])->count();
        }
        $school_area = "'" . join("','", $school_area) . "'";
        $data = implode(',', $total_enroll);

        $this->assign('school', $school_area);
        $this->assign('data', $data);
        $this->display();

    }
}